package bc.swim2.ejb.session;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import bc.swim2.ejb.entity.Feedback;

/**
 * Session Bean implementation class AbilityRequestManagerBean
 */
@Stateless
public class FeedbackManagerBean implements FeedbackManagerBeanRemote {

	@PersistenceContext(unitName = "swimv2")
	private EntityManager em;

	/**
	 * Default constructor.
	 */
	public FeedbackManagerBean() {
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Feedback> getWholeFeedbackList() {

		List<Feedback> feedbacks = null;

		try {
			Query q = em.createNamedQuery("Feedback.getWholeFeedbackList");
			feedbacks = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return feedbacks;
	}

	@Override
	public void insertFeedback(int senderId, int receiverId, boolean score,
			int abilityId, String description) {
		try {
			Feedback newFeedback = new Feedback();
			newFeedback.setAbilityId(abilityId);
			newFeedback.setReceiverId(receiverId);
			newFeedback.setSenderId(senderId);
			newFeedback.setText(description);
			newFeedback.setScore(score);
			em.persist(newFeedback);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Feedback> getFeedbackSentByUsername() {

		List<Feedback> feedbacks = null;

		try {
			Query q = em.createNamedQuery("getFeedbackSentByUsername");
			feedbacks = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return feedbacks;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Feedback> getFeedbackByReceiverId(int userId) {

		List<Feedback> feedbacks = null;

		try {
			Query q = em.createNamedQuery("Feedback.getFeedbackByReceiverId");
			q.setParameter("id", userId);
			feedbacks = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return feedbacks;
	}
}
